<?php
/**
 * Created by PhpStorm.
 * User: asus
 * Date: 2017/8/28
 * Time: 18:07
 */

namespace Wechat\Controller;

/*我是房东前台控制器*/
class FangdongController extends CommonController
{
    /*我是房东主页*/
    public function index()
    {
        $this->display();
    }
    /*我是房东出租 type=1*/
    public function chuzu()
    {
        $post = i('post.');
        if(IS_POST){
            if(trim(empty($post['name']))) {
                $this->error('请输入姓名');
            }
            if(!preg_match("/^1[34578]{1}\d{9}$/",$post['tel'])){
                $this->error('手机号格式不正确');
            }
            if(trim(empty($post['buildname']))) {
                $this->error('请输入小区名称');
            }
            if(trim(empty($post['addres']))) {
                $this->error('请输入小区地址');
            }
            if(trim(empty($post['zujin']))) {
                $this->error('请输入租金');
            }
            $data = [
                'name'=>$post['name'],
                'tel'=>$post['tel'],
                'buildname'=>$post['buildname'],
                'type'=>1,
                'addres'=>$post['addres'],
                'zujin'=>$post['zujin'],
                'huxing'=>$post['shi'].'室'.$post['ting'].'厅'.$post['wei'].'卫',
                'mianji'=>$post['mianji'],
                'zhifufangshi'=>'押'.$post['ya'].'付'.$post['fu'],
                'openid'=>$_SESSION['wechatopenid'],
                'time'=>time(),
                'check'=>1,
            ];
            if(M('fangdong')->add($data)){
                $this->success('发布成功',U('Fangdong/index'));
            }else{
                $this->error('失败，未知错误');
            }
        }else{
            $this->redirect('Wechat/Fangdong/index');
        }
    }
    /*我是房东出售 type=2*/
    public function chushou()
    {
        $post = i('post.');
        if(IS_POST){
            if(trim(empty($post['name']))) {
                $this->error('请输入姓名');
            }
            if(!preg_match("/^1[34578]{1}\d{9}$/",$post['tel'])){
                $this->error('手机号格式不正确');
            }
            if(trim(empty($post['buildname']))) {
                $this->error('请输入小区名称');
            }
            if(trim(empty($post['addres']))) {
                $this->error('请输入小区地址');
            }
            if(trim(empty($post['shoujia']))) {
                $this->error('请输入售价');
            }
            $data = [
                'name'=>$post['name'],
                'tel'=>$post['tel'],
                'buildname'=>$post['buildname'],
                'type'=>2,
                'addres'=>$post['addres'],
                'shoujia'=>$post['shoujia'],
                'huxing'=>$post['shi'].'室'.$post['ting'].'厅'.$post['wei'].'卫',
                'mianji'=>$post['mianji'],
                'openid'=>$_SESSION['wechatopenid'],
                'time'=>time(),
                'check'=>1,
            ];
            if(M('fangdong')->add($data)){
                $this->success('发布成功',U('Fangdong/index'));
            }else{
                $this->error('失败，未知错误');
            }
        }else{
            $this->redirect('Wechat/Fangdong/index');
        }
    }
    /*未租房室友*/
    public function weizushiyou()
    {
        $count      = M('shiyou')->where(['type'=>2])->order('id desc')->count();
        $Page       = new \Think\Page($count,4);
        $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $show       = $Page->show();

        $list = M('shiyou')->where(['type'=>2])->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        foreach ($list as $k=>$v) {
            $time = 3600*24*10;
            $playtime = $v['time'];
            $endtime = $playtime+$time;
            $save = [
                'check'=>0,
            ];
            M('shiyou')->where(['time'=>$endtime])->save($save);
        }
        /*微信自定义分享*/
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        (new WechatController())->zidingfenxiang($url);

        $this->assign(['page'=>$show,'data'=>$list]);
        $this->display();
    }
    /*已租室友*/
    public function yizushiyou()
    {
        $count      = M('shiyou')->where(['type'=>1])->order('id desc')->count();
        $Page       = new \Think\Page($count,4);
        $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $show       = $Page->show();

        $list = M('shiyou')->where(['type'=>1])->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        foreach ($list as $k=>$v) {
            $time = 3600*24*10;
            $playtime = $v['time'];
            $endtime = $playtime+$time;
            $save = [
                'check'=>0,
            ];
            M('shiyou')->where(['time'=>$endtime])->save($save);
        }
        /*微信自定义分享*/
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        (new WechatController())->zidingfenxiang($url);

        $this->assign(['page'=>$show,'data'=>$list]);
        $this->display();
    }
    /*详情页*/
    public function content()
    {
        $id = i('get.id');
        $old = M('shiyou')->where(['id'=>$id])->find();
        $read = $old['read']+1;
        $save = ['read'=>$read];
        M('shiyou')->where(['id'=>$id])->save($save);
        $data = M('shiyou')->where(['id'=>$id])->find();

        /*微信自定义分享*/
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        (new WechatController())->zidingfenxiang($url);

        $this->assign('vo',$data);
        $this->display();
    }
    /*发布室友*/
    public function fabushiyou()
    {
        if(IS_POST){
            $post = i('post.');
            if(trim(empty($post['type']))) {
                $this->error('请选择类别');
            }
            if(trim(empty($post['sex']))) {
                $this->error('请选择性别');
            }
            if(trim(empty($post['shenfen']))) {
                $this->error('请选择身份');
            }
            if(trim(empty($post['age']))) {
                $this->error('请选择年龄');
            }
            if(trim(empty($post['name']))) {
                $this->error('请输入姓名');
            }
            if(trim(empty($post['tel']))) {
                $this->error('请输入发布人手机号');
            }
            if(!preg_match("/^1[34578]{1}\d{9}$/",$post['tel'])){
                $this->error('手机号格式不正确');
            }
            $user = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
            $data = [
                'name'=>$post['name'],
                'tel'=>$post['tel'],
                'type'=>$post['type'],
                'sex'=>$post['sex'],
                'shenfen'=>$post['shenfen'],
                'check'=>1,
                'time'=>time(),
                'age'=>$post['age'],
                'openid'=>$_SESSION['wechatopenid'],
                'headimg'=>$user['headimg'],
                'read'=>1,
            ];
            if(M('shiyou')->add($data)){
                $this->success('发布成功！~',U('Wechat/Fangdong/fabushiyou'));
            }else{
                $this->error('未知错误，请稍后重试！~');
            }
        }else{
            $this->display();
        }
    }
}